package ru.compscicenter.uno.view;

import ru.compscicenter.uno.model.Player;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.LinkedList;

/**
 * Created by IntelliJ IDEA.
 * User: Павел
 * Date: 26.11.11
 * Time: 2:23
 * To change this template use File | Settings | File Templates.
 */
public class OtherPlayerPanel extends JComponent {
    private Player player;

    private int cardsCount = 0;
    private LinkedList<CardImage> cards = new LinkedList<CardImage>();

    PlayerPanel playerPanel;
    ImageButton didNotSayUno;
    ImageButton alreadySayUno;

    private int leftCardX = 20;

    private final static int PLAYER_PANEL_WIDTH = 175;
    private final static int PLAYER_PANEL_HEIGHT = 45;

    public void setDidNotSayUno(ImageButton didNotSayUno) {
        this.didNotSayUno = didNotSayUno;
    }

    public void setAlreadySayUno(ImageButton alreadySayUno) {
        this.alreadySayUno = alreadySayUno;
    }

    public OtherPlayerPanel(Player player) {
        this.player = player;

        setLayout(null);
        addComponentListener(new ComponentAdapter() {
            @Override
            public void componentResized(ComponentEvent e) {
                resizeElements();
            }
        });

        playerPanel = new PlayerPanel(player);
        add(playerPanel);

        update(false);

        //debug-code
        /*Border border = BorderFactory.createLineBorder(Color.BLACK);
             setBorder(border);
           */
    }

    public Player getPlayer() {
        return player;
    }

    public void setPlayer(Player player) {
        this.player = player;
        playerPanel.setPlayer(player);
    }

    public PlayerPanel getPlayerPanel() {
        return playerPanel;
    }

    public CardImage getFirstCard() {
        if (cardsCount > 0) {
            return cards.getFirst();
        }
        return null;
    }

    public void update(boolean isPlayerTurn) {
        if (player.getCardsCount() > cardsCount) {
            for (int i = 0; i < player.getCardsCount() - cardsCount; i++) {
                CardImage card = new CardImage(null);
                cards.add(card);
                add(card);
            }
        } else {
            for (int i = 0; i < cardsCount - player.getCardsCount(); i++) {
                remove(cards.removeLast());
            }
        }
        cardsCount = player.getCardsCount();

        if (player.isSaidUno()) {
            playerPanel.setButton(alreadySayUno);
        } else {
            playerPanel.setButton(didNotSayUno);
        }

        playerPanel.update(isPlayerTurn);
        resizeElements();
        repaint();
    }

    private void resizeElements() {
        playerPanel.setBounds((getWidth() - PLAYER_PANEL_WIDTH) / 2,
                getHeight() - PLAYER_PANEL_HEIGHT,
                PLAYER_PANEL_WIDTH,
                PLAYER_PANEL_HEIGHT);

        int cardHeight = getHeight() - PLAYER_PANEL_HEIGHT - 5;
        int cardWidth = CardImage.getProportionWidth(cardHeight);
        int cardsDelay = cardWidth / 6;

        leftCardX = (getWidth() - (cardWidth + cardsDelay * cardsCount)) / 2;
        for (int i = 0; i < cardsCount; i++) {
            CardImage card = cards.get(i);
            card.setBounds(i * cardsDelay + leftCardX, 0, cardWidth, cardHeight);
        }
    }

    public Point getPlayerPanelLocation() {
        Point myLocation = getLocation();
        Point playerPanelLocation = playerPanel.getLocation();
        return new Point(myLocation.x + playerPanelLocation.x + playerPanel.getWidth() / 2, myLocation.y + playerPanelLocation.y + playerPanel.getHeight() / 2);
    }
}
